package com.minelx.sqlstructures;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.minelx.landisk.filesystem.Paths;
import com.minelx.landisk.filesystem.core.File;
import com.minelx.landisk.filesystem.core.init.OpenPolicy;
import com.minelx.sqlstructures.core.database.DatabaseToJson;
import com.minelx.sqlstructures.core.database.MysqlDatabase;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import static com.minelx.landisk.filesystem.core.init.OpenPolicy.createANewOne;

public class FetchJSONOfRemoteDatabase {

	public static void main(String[] args) {
		fetching(Paths.projectRoot().child().file("2@newpay.json", createANewOne()), "newpay");
//		fetching(Paths.projectRoot().child().file("9@marketing.json", createANewOne()), "marketing");
//		fetching(Paths.projectRoot().child().file("remote_database__micro_finance.json", OpenPolicy.createANewOne()), "micro_finance");
	}

	private static void fetching(File destination, String databaseName) {
		ComboPooledDataSource dataSource = null;
		try {
			dataSource = new ComboPooledDataSource("WARNING_REMOTE");
			MysqlDatabase database = new MysqlDatabase(dataSource, databaseName);
			String result = new DatabaseToJson().toJsonString(database);
			destination
					.write(result, StandardCharsets.UTF_8);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.out.println("let's GO----");
			assert dataSource != null;
			dataSource.close();
		}
	}
}
